An attempt to replicate the code shown here in R.